#include "../head/navmenuform.h"
#include "ui_navmenuform.h"

NavMenuForm::NavMenuForm(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::NavMenuForm)
{
    ui->setupUi(this);
    hf_1 = new HistoryForm_1();
    hf_2 = new HistoryForm_2();
//    wpl = new WorkPlatForm();
    hf = new HistoryForm();
    ddms = new DdmsForm();
    df = new DemoudleForm();
    this->setWindowTitle("主菜单");
   // connect(serialUtil, &SerialUtil::sendToDemoudle, this,&NavMenuForm::acceptMessage);
     connect(serialUtil, &SerialUtil::sendToDemoudle123, this,&NavMenuForm::acceptMessage123);
}

void NavMenuForm::Float_HEX (float fdata, unsigned char *hdata)
{
    unsigned char* tdata = (unsigned char*)(&fdata);
    hdata[0] = tdata[0];
    hdata[1] = tdata[1];
}


void NavMenuForm::acceptMessage123(){
//    FILE *fp;
//    QDateTime curDateTime = QDateTime::currentDateTime();
//    QString save_url = demoduleUtil->baseUrl+"data/"+curDateTime.toString("yyyy-MM-dd hh")+".txt";
//    QTextCodec *code = QTextCodec::codecForName("gb2312");
//    std::string stdStr = code->fromUnicode(save_url).data();
//    char const* save_url_ = stdStr.c_str();
//    fp = fopen(save_url_, "a+");
//    int j = 0;



    for (int i = 0; i < RECEIVE_BUF_SIZE; i++) {
        tempArray[i] = serialUtil->fifoReceiver1.Buf[serialUtil->fifoReceiver1.nRp];

        if(serialUtil->fifoReceiver1.nRp < RECEIVE_SERIAL_BUF_SIZE - 1){
            serialUtil->fifoReceiver1.nRp++;
        }else{
            serialUtil->fifoReceiver1.nRp=0;
        }
 //        fprintf(fp,"%d=%.0f ",serialUtil->fifoReceiver1.nRp,tempArray[i]);
//        j++;
//        if(j == 4){
//           fprintf(fp,"\n");
//           j = 0;
//        }
    }
    //fclose(fp);
    memset(demoduleUtil->DataPrimary, 0, 1000 * sizeof(double));
    double b10dec = 0.0;
    for (int i = 0; i < 1000; i++) {
        b10dec = ((double)(tempArray[i*4])*256.0 + (double)(tempArray[i*4+1])) / 65536.0 * 2.5 - 1.25;
        demoduleUtil->DataPrimary[i] = b10dec;
    }

     serialUtil->fifoReceiver1.nUsedCnt = serialUtil->fifoReceiver1.nUsedCnt - RECEIVE_BUF_SIZE;

    //开始解调
    //qDebug() << "count  ========="<< count <<endl;

    demoduleUtil->demodule();

    count++;
    if((count % 2) == 0){
        if(!df->isHidden())
            df->drawWaveformPlot();
        if(!hf_1->isHidden())
            hf_1->drawWaveformPlot();
        if(!hf_2->isHidden())
            hf_2->drawWaveformPlot();
        if(!ddms->wpl->isHidden()){
            if(demoduleUtil->ParityByte_Enable == 1){
               ddms->wpl->renderData(demoduleUtil->CourseAngle_fy,demoduleUtil->PitchAngle_fy,demoduleUtil->RollAngle_fy);
               demoduleUtil->ParityByte_Enable = 0;
            }
        }
    }


}

NavMenuForm::~NavMenuForm()
{
    delete ui;
}

void NavMenuForm::on_pushButton_clicked()
{
  //  DdmsForm  *ddms = new DdmsForm();

    ddms->show();
//    this->hide();
}

void NavMenuForm::on_pushButton_2_clicked()
{
     //  DemoudleForm *df = new DemoudleForm();
//    flag = 0;
    df->show();
//    wpl->show();
//    this->hide();
}

void NavMenuForm::on_pushButton_3_clicked()
{
    cf->show();
    this->hide();
}


void NavMenuForm::closeEvent(QCloseEvent *e){

}

void NavMenuForm::on_pushButton_4_clicked()
{
//    hf->clearGraph();
    hf_1->show();
    hf_2->show();
    hf->show();
   // ddms->wpl->show();
   // this->hide();
}

void NavMenuForm::on_pushButton_5_clicked()
{
    hf_1->show();
}

void NavMenuForm::on_pushButton_6_clicked()
{
    hf_2->show();
}

void NavMenuForm::showDecData(QString decData){
   // if(decData == "DATA"){
       // ui->textDecData->append("");
   // }else{
       // ui->textDecData->insertPlainText(decData.toUpper()+" ");
   // }
}

void NavMenuForm::showSignalInfo(double singnal){
    QString strSig = QString::number(singnal,'f', 3);
 //    ui->lineEdit->setText("信噪比 = "+ strSig);
}


void NavMenuForm::acceptMessage(QList<double> list1){
        demoduleUtil->list  = list1;
      //  memset(demoduleUtil->tempDataBuffer,0,RECEIVE_BUF_SIZE * sizeof(double));
        memset(demoduleUtil->DataPrimary, 0, 1000 * sizeof(double));
       // memcpy(demoduleUtil->tempDataBuffer,&list1,RECEIVE_BUF_SIZE);

        double b10dec = 0.0;

        for (int i = 0; i < 1000; i++) {
            b10dec = ((double)(demoduleUtil->list[i*4])*256.0 + (double)(demoduleUtil->list[i*4+1])) / 65536.0 * 2.5 - 1.25;
            demoduleUtil->DataPrimary[i] = b10dec;
//            qDebug()<<demoduleUtil->DataPrimary[i]<<endl;
//           if(i==0){
//             qDebug() <<qSetRealNumberPrecision(12)<< "i="<<i<<"  list[i]="<<demoduleUtil->list[i*4]<<" list[i+1]="<<demoduleUtil->list[i*4+1]<<" list[i+2]="<<demoduleUtil->list[i*4+2]<<" list[i+3]="<<demoduleUtil->list[i*4+3]<<endl;
//           }

        }

        //开始解调
        //qDebug() << "count  ========="<< count <<endl;

        demoduleUtil->demodule();

        count++;
        if((count % 2) == 0){
            if(!df->isHidden())
                df->drawWaveformPlot();
            if(!hf_1->isHidden())
                hf_1->drawWaveformPlot();
            if(!hf_2->isHidden())
                hf_2->drawWaveformPlot();
            if(!ddms->wpl->isHidden()){
                if(demoduleUtil->ParityByte_Enable == 1){
                   ddms->wpl->renderData(demoduleUtil->CourseAngle_fy,demoduleUtil->PitchAngle_fy,demoduleUtil->RollAngle_fy);
                   demoduleUtil->ParityByte_Enable = 0;
                }
            }
        }
//        if(count%59 == 0){
//            demoduleUtil->freeParam();
//            demoduleUtil->initParam();
//        }
        demoduleUtil->list.clear();
}

// 未使用
void NavMenuForm::acceptMessage1(FIFO_Receiver fifoReceiver1){
//     //memset(demoduleUtil->tempDataBuffer,0,RECEIVE_BUF_SIZE * sizeof(double));
//      //  memset(demoduleUtil->DataPrimary, 0, 1000 * sizeof(double));
//        for(int i = 0 ; i <RECEIVE_BUF_SIZE;i++){
//            demoduleUtil->tempDataBuffer[i] = fifoReceiver1.Buf[fifoReceiver1.nRp+i];
//            //qDebug() << "fifoReceiver1.Buf[fifoReceiver1.nRp+i]"<< fifoReceiver1.Buf[fifoReceiver1.nRp+i]<<"fifoReceiver1.nRp"<<fifoReceiver1.nRp<<"i"<<i<<endl;
//           // qDebug() <<qSetRealNumberPrecision(12)<< "i="<<i<<"  list[i]="<<demoduleUtil->list[i*4]<<" list[i+1]="<<demoduleUtil->list[i*4+1]<<" list[i+2]="<<demoduleUtil->list[i*4+2]<<" list[i+3]="<<demoduleUtil->list[i*4+3]<<endl;

//        }


//        double b10dec = 0.0;

//        for (int i = 0; i < 1000; i++) {
//            b10dec = ((double)(demoduleUtil->tempDataBuffer[i*4])*256.0 + (double)(demoduleUtil->tempDataBuffer[i*4+1])) / 65536.0 * 2.5 - 1.25;
//            demoduleUtil->DataPrimary[i] = b10dec;
////            qDebug()<<b10dec<<endl;
//            //if(i==0){
//           //   qDebug() <<qSetRealNumberPrecision(12)<< "i="<<i<<"  list[i]="<<demoduleUtil->tempDataBuffer[i*4]<<" list[i+1]="<<demoduleUtil->tempDataBuffer[i*4+1]<<" list[i+2]="<<demoduleUtil->tempDataBuffer[i*4+2]<<" list[i+3]="<<demoduleUtil->tempDataBuffer[i*4+3]<<endl;
//           // }

//        }

//        //开始解调
////        qDebug() << "count  ========="<< count <<endl;

//        demoduleUtil->demodule();

//        df->drawWaveformPlot();

////        if(count%59 == 0){
////            demoduleUtil->freeParam();
////            demoduleUtil->initParam();
////        }
////        demoduleUtil->list.clear();
////        count++;
}
